//
//  HistoryViewController.m
//  LOL视频播放
//
//  Created by 花花蔡 on 2016/12/13.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//
#import "CLLLastSeeViewController.h"
#import "Public.h"
#import "HistroyCollectionViewCell.h"
#import "BaseTabBarController.h"
#import "CoreDataManager.h"
#import "VideoPlayViewController.h"
@interface CLLLastSeeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,CollectionViewCellDelegate>

@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) NSMutableArray *deleteBtnArray;
@property(nonatomic,assign) BOOL isClicked;


@end

static NSString *Identifier = @"collectionViewCellID";

@implementation CLLLastSeeViewController

- (void)viewWillAppear:(BOOL)animated {
    [BaseTabBarController sharedBaseTabBarController].tabBar.hidden = YES;
    [self.collectionView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [BaseTabBarController sharedBaseTabBarController].tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _deleteBtnArray = [NSMutableArray new];
    [self  loadData];
    [self.view addSubview:self.collectionView];
    
    UIBarButtonItem *deleteBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"collection_delete_22x21_@2x的副本"] style:UIBarButtonItemStyleDone target:self action:@selector(deleteData)];
    self.navigationItem.rightBarButtonItem = deleteBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData {
    _dataArray = [NSMutableArray new];
    _dataArray = [[CoreDataManager manager] searchData].mutableCopy;
}

- (void)deleteData {
    if (_isClicked == NO) {
        _isClicked = YES;
    }else
        _isClicked = NO;
    
    if (_isClicked == YES) {
        for (UIButton *btn in _deleteBtnArray) {
            btn.alpha = 1;
        }
    }else {
        for (UIButton *btn in _deleteBtnArray) {
            btn.alpha = 0;
        }
    }
   
    
}
#pragma mark - cell 协议
- (void)reduceArray:(HistroyCollectionViewCell *)cell {
    
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    Model *model = _dataArray[indexPath.row];
    //数据源先删除
    [[CoreDataManager manager] deleteForDataWithModel:model];
    [_dataArray removeObjectAtIndex:indexPath.row];
   // [_dataArray removeObject:model];
   [_deleteBtnArray removeObjectAtIndex:indexPath.row];
   [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
    
    [self.collectionView reloadData];
    
}

#pragma mark - UICollectionViewDelegateFlowLayout 代理方法
//cell 坐标
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

//设置cell大小可以改变一行有几个cell
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((screen_width / 2.21), (screen_width / 2.21));
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return - 15;
}

#pragma mark - collectionView 代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HistroyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Identifier forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    cell.delegate = self;
    [_deleteBtnArray addObject:cell.deleteBtn];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    Model *model = _dataArray[indexPath.row];
    
    VideoPlayViewController *playerVC = [[VideoPlayViewController alloc] init];
    playerVC.urlStr = model.url;
    [self presentViewController:playerVC animated:YES completion:nil];
}

#pragma mark - 懒加载
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //布局方向
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, screen_width, screen_height + 44) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];

        //注册
        [_collectionView registerClass:[HistroyCollectionViewCell class] forCellWithReuseIdentifier:Identifier];
    }
    return _collectionView;
}


@end
